package com.itheima.test;

import com.itheima.domain.Student;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
//初始化一个集合容器,用来管理系统中所用是学生信息
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        lo:
        while (true) {

            System.out.println("-----------欢迎来到召唤师峡谷------------");
            System.out.println("1 添加英雄");
            System.out.println("2 删除英雄");
            System.out.println("3 修改英雄");
            System.out.println("4 查看英雄");
            System.out.println("5 点击投降");
            System.out.println("请输入您的选择:");

            String choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudents(list);
                    break;
                case "5":
                    System.out.println("我点了,你呢");
                    break lo;
                default: {
                    System.out.println("英雄选择有误");
                    break;
                }


            }
        }
    }

    public static void updateStudent(ArrayList<Student> list) {
        System.out.println("请输入您要修改的学生学号:");
        Scanner sc = new Scanner(System.in);
        String updateSid = sc.next();
        //调用getIndex方法,查找该学号在集合中出现的索引位置
        int index = getIndex(list, updateSid);
        //根据索引判断,学号在集合中是否存在
        if (index == -1) {
            System.out.println("查无信息,请重试");
        } else {
            System.out.println("请输入姓名");
            String name = sc.next();
            System.out.println("请输入年龄");
            int age = sc.nextInt();
            System.out.println("请输入生日");
            String birthday = sc.next();


        }


    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        //1.给出录入的提示信息
        System.out.println("请输入学号");
        String sid;
        while (true) {
            sid = sc.next();
            //判断本次录入的学号是否存在
            int index = getIndex(list, sid);
            if (index == -1) {
                break;
            } else {
                //学号已经被别人占用了,这个学号就不能用,需要重新录入
                System.out.println("当前学号已被占用,请重新输入:");
            }
        }

        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();

        //
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功!");

    }

    public static void deleteStudent(ArrayList<Student> list) {
        //1.给出提示信息(请输入您要删除的学号)
        System.out.println("请输入您要删除的学号:");
        //2.键盘接受要删除的学号
        Scanner sc = new Scanner(System.in);
        String deleteSid = sc.next();
        //3.调用getIndex方法,查找该学号在集合中出现的索引位置
        int index = getIndex(list, deleteSid);
        //4.根据索引判断,学号在集合中是否存在
        if (index == -1) {
            //不存在:给出提示
            System.out.println("差无信息,请重试");
        } else {
            //存在:删除
            list.remove(index);
            System.out.println("删除成功!");
        }

    }

    public static void queryStudents(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息,请添加后重新查询");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t生日");

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
        }
    }


    public static int getIndex(ArrayList<Student> list, String sid) {
        //1.假设传入的学号,在集合中不存在
        int index = -1;
        //2.遍历集合,获得每一个学生对象,准备进行查找
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            //3.获取每一个学生对象的学号
            String id = stu.getSid();
            //4.使用获取出的学生学号,和传入的学号(查找的学号)进行对比
            if (id.equals(sid)) {
                //存在:让index变量记录正确的索引位置
                index = i;
            }
        }
        return index;
    }

}
